/*
 * @lc app=leetcode.cn id=128 lang=cpp
 *
 * [128] 最长连续序列
 */

#include <limits.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

// @lc code=start
class Solution {
   public:
    int longestConsecutive(vector<int>& nums) {
        int result = 0;
        set<int> number;

        // 遍历原数组，记录出现的数字
        for (int num : nums) {
            number.insert(num);
        }

        // 遍历集合，寻找相邻数字出现次数之和最大的值
        for (int num : number) {
            int cur = num;
            int sum = 0;

            // 如果当前节点的前一个结点也存在，跳过当前节点
            if (number.count(cur - 1) > 0) {
                continue;
            }

            do {
                cur++;
                sum += 1;
                result = max(result, sum);
            } while (number.count(cur) > 0);
        }

        return result;
    }
};
// @lc code=end
